<template>
	<view id="invoice">
		<view class="texttop">
			<view v-bind:class="{'item':true, 'itm1':isA}" @click="goA">
				申请发票
			</view>
			<view :class="{'item':true, 'itm1':isB}" @click="goB">
				申请记录
			</view>
		</view>
		<view class="" v-if="isShow">
			<view class="top-tit">
				发票金额必须>=500元,2000元以内我们将以到付的形式为您寄出
			</view>
			<checkbox-group @change="change($event,dataList,setD)">
				<label class="itemList" v-for="(item,index) in dataList" :key="index">
					<view class="title">
						交易号:{{item.tradeNo}}
					</view>
					<view class="time">
						支付时间:{{item.payTime}}
					</view>
					<view class="money">
						订单金额:<text>{{item.tradeMoney}}</text>
					</view>
					<view class="icon">
						<checkbox :value="String(item.tradeId)" :checked="checkedArr.includes(String(item.tradeId))" />
					</view>
				</label>
			</checkbox-group>
			<!-- <checkbox-group>
			    <label>
			        <checkbox value="cb" :checked="isA" v-model="setD" />选中
			    </label>
			    <label>
			        <checkbox value="cb" :checked="isA" />未选中
			    </label>
			</checkbox-group>
			<view class="itemList" v-for="(item,index) in dataList" :key="index">
				<view class="title">
					交易号:{{item.tradeNo}}
				</view>
				<view class="time">
					支付时间:{{item.payTime}}
				</view>
				<view class="money">
					订单金额:<text>{{item.tradeMoney}}</text>
				</view>
			</view> -->
		</view>
		<view class="bottom">
			<view class="content">
				<checkbox-group @change="allChoose($event)" class="left">
					<label>
						<checkbox value="all" :class="{'checked':allChecked}" :checked="allChecked?true:false"></checkbox> 全选
					</label>
				</checkbox-group>				
				<button type="primary" @click="step1click">下一步</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {formatDate2} from "../../common/com.js";
	export default {
		data() {
			return {
				isShow:true,
				isA:true,
				isB:false,
				pageNum:1,
				dataList:[],
				setD:'',
				checkedArr:[],
				allChecked:false 
			};
		},
		watch:{
		    },
		onShow() {
			this.listTrade()
		},
		onReachBottom(){
			this.listTrade()
		},
		methods: {
			step1click(){
				for (var i = 0;i < this.setD.length; i++) {
					this.setD[i]=Number(this.setD[i])
				}
				if (this.setD.length < 1) {
					uni.showModal({
					    title: '失败',
					    content: '请选择需要开票的交易',
					    success: function (res) {
					        if (res.confirm) {
					            console.log('用户点击确定');
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
				    return false;
				}
				let res = 0;
				this.dataList.forEach((item) => {
				  this.setD.forEach((item2) => {
				    if (item.tradeId == item2) {
				      res += item.tradeMoney;
				    }
				  });
				});
				if (parseInt(res) < 500) {
					uni.showModal({
					    content: '发票金额不能低于500元,请谅解',
					    success: function (res) {
					        if (res.confirm) {
					        } else if (res.cancel) {
					        }
					    }
					});
				  return false;
				}      
				console.log(res);
			},
			allChoose(e){
				let chooseItem = e.detail.value;
				// 全选
				if(chooseItem[0]=='all'){
					this.allChecked=true;
					for(let item of this.dataList){
						let itemVal=String(item.tradeId);
						if(!this.checkedArr.includes(itemVal)){
							this.checkedArr.push(itemVal);
						}
					}
					console.log(this.checkedArr );
				}else{
					// 取消全选
					this.allChecked=false;
					this.checkedArr=[];
				}
				this.setD=this.checkedArr
			},
			change(e,list,name){
							var items = list
							let values = e.detail.value
							this.setD=values
						},
			goA(){
				this.isA=true
				this.isB=false
				this.isShow=true
			},
			goB(){
				this.isB=true
				this.isA=false
				this.isShow=false
			},
			listTrade(){
				let that = this;
					this.$store
					.dispatch("ListTrade",{pageNum:this.pageNum})
					.then((response) => {
						if(response.data.code==0){
							let tar=response.data.obj.list
							tar.forEach((item) => {
							    item.payTime = formatDate2(item.payTime, 2);
							});
							that.dataList=[...that.dataList, ...tar];
							that.pageNum++;
						}
					})
					.catch(() => {});
			}
		},
	}
</script>

<style lang="less" scoped>
	#invoice{
		.texttop{
			margin-bottom: 30rpx;
			.item{
				display: inline-block;
				width: 50%;
				text-align: center;
				padding: 20rpx 0;
			}
			.itm1{
				color: #007AFF;		
				border-bottom: 2rpx solid #007AFF;
			}
		}
		.top-tit{
			background: #fff7dd;
			color: #999999;
			font-size: 24rpx;
			text-align: center;
		}
		.bottom{
			width: 100%;
			height: 120rpx;
			background: #FFFFFF;
			position: fixed;
			bottom: 0;
			left:0%;
			.content{
				width: 80%;
				height: 100rpx;
				margin: 0 auto !important;
			}
			.left{
				float: left;
				width: 50%;
				margin-top: 30rpx;
			}
			button{
				width: 50%;
				margin-top: 10rpx;
				float: right;
			}
		}
		uni-checkbox-group{
			width: 92%;
			margin: 0 auto;
			background: #FFFFFF;
		}
		.itemList{
			display: block;
		    border: 2rpx solid #eaebee;
		    border-radius: 16rpx;
		    background: #fff;
		    position: relative;
		    margin-top: 18rpx;
			.icon{
				position: relative;
				top: -184rpx;
				left: 96rpx;
			}
			.title{
			    padding: 20rpx 76rpx;
			    font-size: 26rpx;
			    font-family: PingFang-SC-Bold;
			    font-weight: bold;
			    color: #222;
			    background: url() no-repeat 30rpx center #eaecf2;
			    background-size: 28rpx;	
			}
			.time{
			    font-size: 26rpx;
			    font-family: PingFang-SC-Regular;
			    font-weight: 400;
			    color: #333;
			    line-height: 80rpx;
			    padding: 30rpx 80rpx;
				padding-left: 150rpx;
			    border-bottom: 2rpx solid #e5e5e5;	
			}
			.money{
			    padding: 25rpx 40rpx;
			    font-size: 24rpx;
			    font-family: PingFang-SC-Regular;
			    font-weight: 400;
			    color: #999;
			    line-height: 38rpx;
			    text-align: right;	
				text{
				    font-size: 28rpx;
				    font-family: PingFang-SC-Regular;
				    font-weight: 400;
				    color: #486ef5;
				    line-height: 38rpx;
				    margin-left: 10rpx;	
				}
			}
		}
	}
	
</style>
